package tool.data;

    /**
     * @author jwb09119
     * @date 2013/12/3
     * 
     * This class represents a definition of a specific type specified in Java source code.
     * The Type contains a name and Package address and various useful assessor methods.
     * The class is intended to be cumulative in that information about a Type may accrue over 
     * time, but no data about the Type should change after it has been set.
     */

public class TypeDefinition {
    
    private String name;
    private String path;
    
    // Constructor
    public TypeDefinition (String typeName) {
        
        name = typeName;
        path = null;
        
        if (name.startsWith("PRIMITIVE.")) {
            
            path = "";
            
        }
        
    }
     
    // Constructor
    public TypeDefinition (String typeName, String packageAddress) {
        
        name = typeName;
        
        if (name.startsWith("PRIMITIVE.")) {
            
            path = "";
            
        } else {
        
            path = packageAddress;
        
        }
        
    }
    
    public String getName () {
        
        return new String(name);
        
    }
    
    
    public String getNameWithPath () {
        
        if (name.startsWith("PRIMITIVE")) {
            
            return new String(name);
            
        } else {
        
            return new String (getPath() + "." + name);
            
        }
        
    }
    
    
    public boolean hasResolvedPath() {
        
        return path != null;
        
    }
    
    
    public String getPath () {
        
        if (path ==  null) {
            
            return "UNRESOLVED_PATH";
            
        } else {
            
            return path;
            
        }
        
    }
    
    
    public boolean setPath (String newPath) {
        
        if (path == null && !name.startsWith("PRIMITIVE.")) {
            
            path = newPath;
            return true;
            
        } else {
            
            return false;
            
        }
        
    }
        
        
    @Override
    public String toString () {
        
        return getNameWithPath();
        
    }

    
    public void addInnerClassPrefix () {

        name = "INNERCLASS." + name;
        
    }
        
        
    /**
     * Creates a full path name for an inner class from the name of the inner class and the full
     * path name of the containing type
     * @param innerClassName - the name of the inner class in the form "InnerType"
     * @param containingClassName - the name of the containing class in the form "Type"
     * @param packageName  - the 'address' of the containing class in the form "package.package"
     * @return - String - the 'address' of the inner class in the form 
     *                    "path.Type.INNERCLASS.InnerType"
     */
    public static String createInnerClassFullName (String innerClassName, String containingClassName, String packageName) {

        return packageName + "." + containingClassName + ".INNERCLASS." + innerClassName;
        
    }


    /**
     * Creates a full path name for an inner class from the name of the inner class and the full
     * path name of the containing type
     * @param innerClassName - the name of the inner class in the form "InnerType"
     * @param containingClassName - the name of the containing class in the form "Type"
     * @param packageName  - the 'address' of the containing class in the form "package.package"
     * @return - {@link TypeDefinition} - an object representing the the 'address' of the inner 
     *                                    class in the form.
     */
    public static TypeDefinition createInnerClassDefinition (String innerClassName, String containingClassName, String packageName) {

        return new TypeDefinition("INNERCLASS." + innerClassName, packageName + "." + containingClassName);
        
    }


    
}
